package com.vxb.testmvp.app;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.vxb.testmvp.R;
import com.vxb.testmvp.materialdesign.MaterialActivity;
import com.vxb.testmvp.materialdesign_recyclerview.RecyclerActivity;
import com.vxb.testmvp.materialdesign_zhihu.ZhiHuHomeActivity;
import com.vxb.testmvp.mvp_login.LoginActivity;
import com.vxb.testmvp.mvp_test.UserInfoActivity;
import com.vxb.testmvp.test_rxjava.TestRxJaveActivity;
import com.vxb.testmvp.test_fragment.activity.TestActivity;
import com.vxb.testmvp.test_jsmap.TestJsMapActivity;
import com.vxb.testmvp.test_retrofit.TestRetrofitActivity;
import com.vxb.testmvp.test_zonghe.TestMainActivity;
import com.vxb.testmvp.util.XPermissionUtils;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<String> mDatas;
    private MainAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // setTheme(R.style.AppTheme);
        //  getWindow().setBackgroundDrawable(null);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDatas = new ArrayList<String>();
        mDatas.add("登陆mvp");
        mDatas.add("测试mvp");
        mDatas.add("materialdesign 控件基本演示");
        mDatas.add("materialdesign 仿知乎页面");
        mDatas.add("materialdesign recycler");
        mDatas.add("测试 retrofit");
        mDatas.add("测试 fragment");
        mDatas.add("测试 jsmap");
        mDatas.add("测试 rxjava");
        mDatas.add("测试 综合网络,刷新,错误页面");


        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new MainAdapter(this, mDatas);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickLitener(new MainAdapter.OnItemClickLitener() {

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " click",
                        Toast.LENGTH_SHORT).show();

                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, UserInfoActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(MainActivity.this, MaterialActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(MainActivity.this, ZhiHuHomeActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, RecyclerActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, TestRetrofitActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(MainActivity.this, TestActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(MainActivity.this, TestJsMapActivity.class));
                        break;
                    case 8:
                        startActivity(new Intent(MainActivity.this, TestRxJaveActivity.class));
                        break;
                    case 9:
                        startActivity(new Intent(MainActivity.this, TestMainActivity.class));
                        break;
                }

            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();
            }
        });
        initWriteStrongePermission();
    }

    int CODE_EXTERNAL_REQUEST = 10000;

    private void initWriteStrongePermission() {

        XPermissionUtils.requestPermissions(MainActivity.this, CODE_EXTERNAL_REQUEST, new String[]{Manifest
                        .permission.WRITE_EXTERNAL_STORAGE}
                , new XPermissionUtils.OnPermissionListener() {
                    @Override
                    public void onPermissionGranted() {
                    }

                    @Override
                    public void onPermissionDenied(String[] deniedPermissions) {
                        Toast.makeText(MainActivity.this, "获取存储权限失败", Toast.LENGTH_SHORT).show();
                        if (XPermissionUtils.hasAlwaysDeniedPermission(MainActivity.this, deniedPermissions)) {
                            showPermissionManagerDialog(MainActivity.this, "存储");//权限名称
                            isOnPause = false;
                        }

                    }
                }, new XPermissionUtils.RationaleHandler() {

                    @Override
                    protected void showRationale() {
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("温馨提示")
                                .setMessage("我们需要读写权限才能正常使用该功能")
                                //.setNegativeButton("取消", null)
                                .setCancelable(false)
                                .setPositiveButton("验证权限", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        requestPermissionsAgain();
                                    }
                                }).show();
                    }
                });

    }


    public void showPermissionManagerDialog(final Context context, String str) {
        new AlertDialog.Builder(context)
                .setTitle("获取" + str + "权限被禁用")
                .setMessage("请在 设置-应用管理-" + context.getString(R.string.app_name) + "-权限管理 (将" + str + "权限打开)")
                //   .setNegativeButton("取消", null)
                .setCancelable(false)
                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + context.getPackageName()));
                        context.startActivity(intent);
                    }
                }).show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        XPermissionUtils.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    boolean isOnPause = false;

    @Override
    protected void onResume() {
        if (isOnPause) {
            initWriteStrongePermission();
            isOnPause = false;
        }
        super.onResume();
    }

    @Override
    protected void onPause() {

        isOnPause = true;
        super.onPause();
    }


}
